<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit','Approval'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                审核
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('parts.approval.id')" type="string" v-model="baTable.form.items!.id" prop="id" :input-attr="{ step: 1 ,readonly:true}" :placeholder="t('Please input field', { field: t('parts.approval.id') })" />
                    <FormItem :label="t('parts.approval.approval_type')" type="select" v-model="baTable.form.items!.approval_type" :input-attr="{ step: 1 ,readonly:true}" prop="approval_type" :data="{ content: { '0': t('parts.approval.approval_type 0'), '1': t('parts.approval.approval_type 1'), '2': t('parts.approval.approval_type 2'), '3': t('parts.approval.approval_type 3') } }" :placeholder="t('Please select field', { field: t('parts.approval.approval_type') })" />
                    <FormItem :label="t('parts.approval.approval_status')" type="select" v-model="baTable.form.items!.approval_status" :input-attr="{ step: 1 ,readonly:true}" prop="approval_status" :data="{ content: { '0': t('parts.approval.approval_status 0'), '1': t('parts.approval.approval_status 1'), '2': t('parts.approval.approval_status 2') } }" :placeholder="t('Please select field', { field: t('parts.approval.approval_status') })" />
                    <FormItem :label="t('parts.approval.remark')" type="textarea" v-model="baTable.form.items!.remark" prop="remark" :input-attr="{ rows: 3,readonly:true }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('parts.approval.remark') })" />
                    <template style="display: flex;">
                      <div >
                        <div v-if="baTable.form.items.approval_type == '0'||baTable.form.items.approval_type == '2'" style="text-align: center;margin-bottom: 10px;">修改前：</div>
                        <form-item label="配件名称" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.parts_name" type="string"/>
                        <form-item label="配件编码" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.parts_code" type="string"/>
                        <form-item label="配件图片" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.parts_img" type="image"/>
                        <form-item v-if="baTable.form.items.approval_type == '2'||baTable.form.items.approval_type == '3'" label="厂家名称" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.manufacturer_name" type="string"/>
                        <form-item label="配件成本" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.parts_cost" type="string"/>
                        <form-item label="建议售价" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.parts_money" type="string"/>
                        <form-item label="库存数量" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.entry_nur" type="string"/>
                        <form-item label="入库时间" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.entry_date" type="string"/>
                        <form-item label="备注" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.old!.remark" type="textarea"/>
                      </div>
                      <div v-if="baTable.form.items.approval_type == '0'||baTable.form.items.approval_type == '2'" >
                        <div style="text-align: center;margin-bottom: 10px;">修改后：</div>
                        <form-item :style="baTable.form.extend.color.parts_name" label="配件名称" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.parts_name" type="string"/>
                        <form-item :style="baTable.form.extend.color.parts_code" label="配件编码" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.parts_code" type="string"/>
                        <form-item :style="baTable.form.extend.color.parts_img" label="配件图片" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.parts_img" type="image"/>
                        <form-item :style="baTable.form.extend.color.manufacturer_name" v-if="baTable.form.items.approval_type == '2'||baTable.form.items.approval_type == '3'" label="厂家名称" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.manufacturer_name" type="string"/>
                        <form-item :style="baTable.form.extend.color.parts_cost" label="配件成本" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.parts_cost" type="string"/>
                        <form-item :style="baTable.form.extend.color.parts_money" label="建议售价" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.parts_money" type="string"/>
                        <form-item :style="baTable.form.extend.color.entry_nur" label="库存数量" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.entry_nur" type="string"/>
                        <form-item :style="baTable.form.extend.color.entry_date" label="入库时间" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.entry_date" type="string"/>
                        <form-item :style="baTable.form.extend.color.remark" label="备注" :input-attr="{ step: 1 ,readonly:true}" v-model="baTable.form.extend.new!.remark" type="textarea"/>
                      </div>
                    </template>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="RejectApproval" type="danger">拒绝</el-button>
                <el-button @click="AgreeToApprove" type="primary">通过</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from "/@/utils/axios";

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    create_time: [buildValidatorData({ name: 'date', title: t('parts.approval.create_time') })],
})

//同意审批
const AgreeToApprove = function (){
  createAxios(
      {
        url: "/admin/parts.approval/Approval",
        method: 'GET',
        params: {
          id:baTable.form.items.id,
          type:1,
          approval_type:baTable.form.items.approval_type
        },
      },
      {
        showSuccessMessage: true,
      }
  ).then((res:any)=>{
    if (res.code == 1){
      baTable.getIndex()
      baTable.toggleForm();
    }
  })
}
//拒绝审批
const RejectApproval = function (){
  createAxios(
      {
        url: "/admin/parts.approval/Approval",
        method: 'GET',
        params: {
          id:baTable.form.items.id,
          type:2,
          approval_type:baTable.form.items.approval_type
        },
      },
      {
        showSuccessMessage: true,
      }
  ).then((res:any)=>{
    if (res.code == 1){
      baTable.getIndex()
      baTable.toggleForm();
    }
  })
}
</script>

<style scoped lang="scss">

</style>
